<template>
  <div id="recruitlist">
    <el-dialog title="填报说明" :visible.sync="editFormVisible" width="850px" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="editForm" label-width="200px" ref="editForm" :inline="true" style="padding:10px 0 0;clear:both" inline-message :rules="editFormRules">
        <el-form-item prop="remarks">
          <el-input v-model="editForm.remarks" type="textarea" style="width:800px" :rows="10" :maxlength="1000" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click.native="editFormSubmit1" :loading="editLoading">保存</el-button>
        <el-button @click.native="editFormVisible = false">取消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="退休时间说明" :visible.sync="editFormVisible2" width="850px" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="editForm2" label-width="200px" ref="editForm2" :inline="true" style="padding:10px 0 0;clear:both" inline-message :rules="editFormRules2">
        <el-form-item prop="retireeDateRemarks">
          <el-input v-model="editForm2.retireeDateRemarks" style="width:300px" :maxlength="20"></el-input> 如：截止7月底
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click.native="editFormSubmit2" :loading="editLoading">保存</el-button>
        <el-button @click.native="editFormVisible2 = false">取消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="招聘公告参考材料说明" :visible.sync="editFormVisible3" width="850px" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="editForm3" label-width="200px" ref="editForm3" :inline="true" style="padding:10px 0 0;clear:both" inline-message :rules="editFormRules3">
        <el-form-item prop="announcementRemarks">
          <el-input v-model="editForm3.announcementRemarks" type="textarea" style="width:800px" :rows="10" :maxlength="1000" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click.native="editFormSubmit3" :loading="editLoading">保存</el-button>
        <el-button @click.native="editFormVisible3 = false">取消</el-button>
      </div>
    </el-dialog>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/recruit/main' }">公开招聘</el-breadcrumb-item>
      <el-breadcrumb-item>招聘工作年度设置</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs">
    <!--工具条-->
      <div>
        <el-date-picker
          v-model="theYear"
          type="year"
          placeholder="选择年度"
          value-format="yyyy">
        </el-date-picker>
        <el-button icon="el-icon-plus" type="success" @click="editFormSubmit" :loading="btnLoading" style="margin-right:20px">新增招聘工作年度并设为当前年度</el-button>
        <span v-if="yearList&&yearList.find(item=>item.isCurrent)">当前招聘工作年度：{{yearList.find(item=>item.isCurrent).theYear}}年</span>
        <span v-else>未设置当前招聘工作年度，填报单位用户界面公开招聘模块暂时关闭</span>
      </div>
    </section>
    <section class="wboxd" style="margin-top:10px">
      <!--列表-->
      <el-table :data="yearList" highlight-current-row v-loading="listLoading" style="width: 100%;border-top:1px solid #9CA3B3" :height="tableHeight" border id="selectionindexlist" row-key="personId"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column label="年度" align="center" prop="theYear" width="100" />
        <el-table-column prop="isCurrent" label="是否当前招聘工作年度" width="200" align="center">
          <template slot-scope="scope">
            {{scope.row.isCurrent===1?'是':'否'}}
          </template>
        </el-table-column>
        <el-table-column label="填报说明" align="left" prop="remarks" width="300">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="修改填报说明">
              <el-button type="text" size="small" @click="handleEdit(scope.row)" icon="el-icon-edit" />
            </el-tooltip>
            <div v-if="scope.row.remarks" v-html="scope.row.remarks.replace(/\n/g,'<br>')"></div>
          </template>
        </el-table-column>
        <el-table-column label="退休时间说明" align="left" prop="retireeDateRemarks" width="200">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="修改退休时间说明">
              <el-button type="text" size="small" @click="handleEdit2(scope.row)" icon="el-icon-edit" />
            </el-tooltip>
            {{scope.row.retireeDateRemarks}}
          </template>
        </el-table-column>
        <el-table-column label="招聘公告参考材料" align="left" prop="fileList">
          <template slot-scope="scope">
            <div>
              <el-tooltip class="item" effect="dark" content="修改招聘公告参考材料说明">
                <el-button type="text" size="small" @click="handleEdit3(scope.row)" icon="el-icon-edit" />
              </el-tooltip>
              <div v-if="scope.row.announcementRemarks" v-html="scope.row.announcementRemarks.replace(/\n/g,'<br>')"></div>
            </div>
            <div>
              <el-upload
                style="width:800px;margin-bottom:10px;"
                ref="upload"
                :action="$http.defaults.baseURL+$http.defaults.recruitAPI +'api/recruitGenFileInfo/upload'"
                :on-success="function (res, file) { return handleUploadSucc1(res, file, scope.row.id)}"
                :on-error="uploadError"
                :before-upload="beforeUpload"
                accept="application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-works,.wps"
                :show-file-list="false"
              >
                <el-button type="primary" size="small" :loading="saveloading">上传</el-button>
                <!-- <div slot="tip" class="el-upload__tip">注：支持上传word/pdf/wps文件</div>
                <div style="clear:both" /> -->
              </el-upload>
            </div>
            <template v-if="scope.row.fileList&&scope.row.fileList.length>0">
              <div v-for="(item,index) in scope.row.fileList" :key="index" style="line-height:40px">
                <a href="javascript:void(0)" @click="download(item.id)"><i class="el-icon-paperclip" /> {{item.fileName}}.{{item.fileType}}</a>
                <el-button :loading="listLoading" style="margin-left:10px" size="mini" type="danger" @click="beforeRemove(item)">删除</el-button>
              </div>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="落户备案参考材料" align="left" prop="fileList2">
          <template slot-scope="scope">
            <div>
              <el-upload
                style="width:800px;margin-bottom:10px;"
                ref="upload"
                :action="$http.defaults.baseURL+$http.defaults.recruitAPI +'api/recruitGenFileInfo/upload'"
                :on-success="function (res, file) { return handleUploadSucc2(res, file, scope.row.id)}"
                :on-error="uploadError"
                :before-upload="beforeUpload"
                accept="application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-works,.wps"
                :show-file-list="false"
              >
                <el-button type="primary" size="small" :loading="saveloading">上传</el-button>
                <!-- <div slot="tip" class="el-upload__tip">注：支持上传word/pdf/wps文件</div>
                <div style="clear:both" /> -->
              </el-upload>
            </div>
            <template v-if="scope.row.fileList2&&scope.row.fileList2.length>0">
              <div v-for="(item,index) in scope.row.fileList2" :key="index" style="line-height:40px">
                <a href="javascript:void(0)" @click="download(item.id)"><i class="el-icon-paperclip" /> {{item.fileName}}.{{item.fileType}}</a>
                <el-button :loading="listLoading" style="margin-left:10px" size="mini" type="danger" @click="beforeRemove(item)">删除</el-button>
              </div>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <template>
              <el-button type="success" size="small" v-if="!scope.row.isCurrent" @click="handleSetCurrent(scope.row.theYear,1)" :loading="btnLoading" style="margin:10px 0">设为当前招聘工作年度</el-button>
              <el-button type="primary" size="small" v-else @click="handleSetCurrent(scope.row.theYear,0)" :loading="btnLoading" style="margin:10px 0">取消当前招聘工作年度</el-button>
              <el-button type="danger" size="small" @click="handleDel(scope.row.id)" :loading="btnLoading" style="margin:10px 0">删除</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </section>
  </div>
</template>

<script>
export default {
  data () {
    return {
      saveloading: false,
      editLoading: false,
      dialogVisible: false,
      editFormVisible: false,
      editForm: {
        id: null,
        remarks: ''
      },
      editFormRules: {
        remarks: [
          { required: true, message: '必填', trigger: 'blur' }
        ]
      },
      editFormVisible2: false,
      editForm2: {
        id: null,
        retireeDateRemarks: ''
      },
      editFormRules2: {
        retireeDateRemarks: [
          { required: true, message: '必填', trigger: 'blur' }
        ]
      },
      tableHeight: 100,
      btnLoading: false,
      theYear: null,
      listLoading: false,
      yearList: [],
      editFormVisible3: false,
      editForm3: {
        id: null,
        announcementRemarks: ''
      },
      editFormRules3: {
        announcementRemarks: [
          { required: true, message: '必填', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    beforeUpload (file) {
      const isFormat = file.name.indexOf('.docx') > 0 || file.name.indexOf('.doc') > 0 || file.name.indexOf('.pdf') > 0 || file.name.indexOf('.DOCX') > 0 || file.name.indexOf('.DOC') > 0 || file.name.indexOf('.PDF') > 0 || file.name.indexOf('.wps') > 0 || file.name.indexOf('.WPS') > 0
      const isLt500M = file.size / 1024 / 1024 < 60
      if (!isLt500M) {
        this.$message.error('单个附件不能超过60M!')
      } else {
        if (!isFormat) {
          this.$message.error('附件格式必须为word、pdf、wps!')
        } else {
          this.saveloading = true
        }
      }
      return isFormat && isLt500M
    },
    async handleUploadSucc1 (response, file, id) {
      this.uploadSuccCommon(response, file, id, 'bus_annual')
    },
    async handleUploadSucc2 (response, file, id) {
      this.uploadSuccCommon(response, file, id, 'bus_annual2')
    },
    uploadSuccCommon (response, file, id, str) {
      if (response.success) {
        let para = {
          id: response.result[0].id,
          businessTable: str,
          businessId: id
        }
        this.$http.post(this.$http.defaults.recruitAPI + `api/recruitGenFileInfo/save`, para).then(res => {
          this.saveloading = false
          if (res.data.success === true) {
            this.$message({
              message: '上传成功！',
              type: 'success'
            })
            this.getList()
          } else {
            console.log('上传失败:' + res.data.message)
            this.$message({
              message: '上传失败:' + res.data.message,
              type: 'error'
            })
          }
        })
          .catch(error => {
            this.saveloading = false
            console.log(error)
            this.$message({
              message: '上传失败:' + error,
              type: 'error'
            })
          })
      } else {
        this.saveloading = false
        this.$message({
          message: '上传失败:' + response.message,
          type: 'error'
        })
      }
    },
    beforeRemove (file) {
      this.$confirm('确定删除' + file.fileName + '.' + file.fileType + '？', '提示', {
        type: 'warning'
      }).then(() => {
        this.handleRemove(file.id)
      })
    },
    handleRemove (id) {
      const url = 'api/recruitGenFileInfo/remove?id=' + id
      this.listLoading = true
      this.$http.get(this.$http.defaults.recruitAPI + url).then(res => {
        console.log('删除成功:', res)
        this.listLoading = false
        if (res.data.success === true) {
          this.getList()
          this.$message({
            message: '删除成功！',
            type: 'success'
          })
        }
      })
    },
    uploadError (err) {
      this.saveloading = false
      console.log(err)
      this.$message({
        message: '上传失败:' + err,
        type: 'error'
      })
    },
    download (id) {
      window.open(this.$http.defaults.baseURL + this.$http.defaults.recruitAPI + 'api/recruitGenFileInfo/download?id=' + id)
    },
    handleEdit (obj) {
      this.editFormVisible = true
      this.editForm.id = obj.id
      this.editForm.remarks = obj.remarks
    },
    handleEdit2 (obj) {
      this.editFormVisible2 = true
      this.editForm2.id = obj.id
      this.editForm2.retireeDateRemarks = obj.retireeDateRemarks
    },
    handleEdit3 (obj) {
      this.editFormVisible3 = true
      this.editForm3.id = obj.id
      this.editForm3.announcementRemarks = obj.announcementRemarks
    },
    handleDel (id) {
      this.$confirm('确认删除该招聘工作年度吗？删除年度并不影响已填报数据', '提示', {
        type: 'warning'
      }).then(() => {
        let para = {ids: [id]}
        this.btnLoading = true
        this.$http.post(this.$http.defaults.recruitAPI + 'api/recruitBusAnnual/remove', para).then(response => {
          if (response.data.success === true) {
            this.btnLoading = false
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            this.btnLoading = false
            console.log('删除失败:' + response.data.message)
            this.$message({
              message: '删除失败:' + response.data.message,
              type: 'error'
            })
          }
          this.getList()
        })
          .catch(error => {
            this.btnLoading = false
            console.log(error)
            this.$message({
              message: '删除失败:' + error,
              type: 'error'
            })
          })
      })
    },
    handleSetCurrent (theYear, isCurrent) {
      this.btnLoading = true
      let para = {
        theYear: theYear,
        isCurrent: isCurrent
      }
      this.$http.post(this.$http.defaults.recruitAPI + `api/recruitBusAnnual/save`, para).then(response => {
        console.log(response.data.success)
        if (response.data.success === true) {
          this.$message({
            message: '设置成功',
            type: 'success'
          })
          this.btnLoading = false
          this.getList()
        } else {
          console.log('设置失败:' + response.data.message)
          this.$message({
            message: '设置失败:' + response.data.message,
            type: 'error'
          })
          this.btnLoading = false
        }
      }).catch(error => {
        console.log(error)
        this.$message({
          message: '设置失败:' + error,
          type: 'error'
        })
        this.btnLoading = false
      })
    },
    editFormSubmit1 () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          let para = {
            id: this.editForm.id,
            remarks: this.editForm.remarks
          }
          this.editLoading = true
          this.$http.post(this.$http.defaults.recruitAPI + `api/recruitBusAnnual/saveRemarks`, para).then(response => {
            console.log(response.data.success)
            if (response.data.success === true) {
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.editForm.id = null
              this.editForm.remarks = ''
              this.editLoading = false
              this.getList()
              this.editFormVisible = false
            } else {
              console.log('保存失败:' + response.data.message)
              this.$message({
                message: '保存失败:' + response.data.message,
                type: 'error'
              })
              this.editLoading = false
            }
          }).catch(error => {
            console.log(error)
            this.$message({
              message: '保存失败:' + error,
              type: 'error'
            })
            this.editLoading = false
          })
        }
      })
    },
    editFormSubmit2 () {
      this.$refs.editForm2.validate((valid) => {
        if (valid) {
          let para = {
            id: this.editForm2.id,
            retireeDateRemarks: this.editForm2.retireeDateRemarks
          }
          this.editLoading = true
          this.$http.post(this.$http.defaults.recruitAPI + `api/recruitBusAnnual/updateRetireeDateRemarks`, para).then(response => {
            console.log(response.data.success)
            if (response.data.success === true) {
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.editForm2.id = null
              this.editForm2.retireeDateRemarks = ''
              this.editLoading = false
              this.getList()
              this.editFormVisible2 = false
            } else {
              console.log('保存失败:' + response.data.message)
              this.$message({
                message: '保存失败:' + response.data.message,
                type: 'error'
              })
              this.editLoading = false
            }
          }).catch(error => {
            console.log(error)
            this.$message({
              message: '保存失败:' + error,
              type: 'error'
            })
            this.editLoading = false
          })
        }
      })
    },
    editFormSubmit3 () {
      this.$refs.editForm3.validate((valid) => {
        if (valid) {
          let para = {
            id: this.editForm3.id,
            announcementRemarks: this.editForm3.announcementRemarks
          }
          this.editLoading = true
          this.$http.post(this.$http.defaults.recruitAPI + `api/recruitBusAnnual/updateAnnouncementRemarks`, para).then(response => {
            console.log(response.data.success)
            if (response.data.success === true) {
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.editForm3.id = null
              this.editForm3.retireeDateRemarks = ''
              this.editLoading = false
              this.getList()
              this.editFormVisible3 = false
            } else {
              console.log('保存失败:' + response.data.message)
              this.$message({
                message: '保存失败:' + response.data.message,
                type: 'error'
              })
              this.editLoading = false
            }
          }).catch(error => {
            console.log(error)
            this.$message({
              message: '保存失败:' + error,
              type: 'error'
            })
            this.editLoading = false
          })
        }
      })
    },
    editFormSubmit () {
      if (!this.theYear) {
        this.$message({
          message: '请先选择年度',
          type: 'error'
        })
        return false
      }
      if (this.yearList.find(item => item.theYear === parseInt(this.theYear))) {
        this.$message({
          message: '该招聘年度已存在',
          type: 'error'
        })
        return false
      }
      this.btnLoading = true
      let para = {
        theYear: this.theYear,
        isCurrent: 1
      }
      this.$http.post(this.$http.defaults.recruitAPI + `api/recruitBusAnnual/save`, para).then(response => {
        console.log(response.data.success)
        if (response.data.success === true) {
          this.$message({
            message: '设置成功',
            type: 'success'
          })
          this.theYear = null
          this.btnLoading = false
          this.getList()
        } else {
          console.log('设置失败:' + response.data.message)
          this.$message({
            message: '设置失败:' + response.data.message,
            type: 'error'
          })
          this.btnLoading = false
        }
      }).catch(error => {
        console.log(error)
        this.$message({
          message: '设置失败:' + error,
          type: 'error'
        })
        this.btnLoading = false
      })
    },
    getList () {
      this.listLoading = true
      this.$http
        .get(this.$http.defaults.recruitAPI + `api/recruitBusAnnual/queryList`)
        .then(response => {
          this.listLoading = false
          this.yearList = response.data.result
        })
        .catch(error => {
          this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
          this.listLoading = false
        })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      obj.$nextTick(function () {
      // obj.tableHeight = clientHeight - 292
        obj.tableHeight = clientHeight - 222
      })
    }
  },
  mounted () {
    this.getList()
    this.tableresize(this)
    let _this = this
    window.onresize = function windowResize () {
      // 通过捕获系统的onresize事件触发我们需要执行的事件
      _this.tableresize(_this)
    }
  }
}
</script>
<style>
.selectioncontent{
  border-bottom:1px solid #333;
  padding:5px !important;
}
  .pointtab .el-badge__content.is-fixed{
    right: -5px;
    top:18px;
  }
  .pointtab{
    padding-right:22px;
  }
  .pointRadio .el-badge__content.is-fixed{
    right: -5px;
    top:6px;
  }
  .pointRadio .el-radio-button__inner{
    height:40px;
    padding-left:15px;
    padding-right:42px;
  }
#recruitlist .el-table--border td, .el-table--border th, .el-table__body-wrapper .el-table--border.is-scrolling-left~.el-table__fixed{
  border-bottom:1px solid #95B8E7;
}
#recruitlist .el-table td, #recruitlist .el-table th.is-leaf{
  border-right:1px solid #95B8E7;
}
  .wboxd .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    text-align: center;
    border-right: #95B8E7 solid 1px;
    border-bottom: #95B8E7 solid 1px;
    min-height: 30px;
    line-height:30px;
  }
  .wboxd .el-table td{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体',
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #recruitlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  #recruitlist .el-table__row{
    height: 50px;
  }
  #recruitlist #legent{
    float:right;
    margin-right: 20px;
  }
  #recruitlist #legent div{
    float:left;
    line-height: 42px;
    font-size:var(--size12);
  }
  #recruitlist #legent i{
    width: 30px;
    height: 30px;
    background-position: center center;
    background-repeat: no-repeat;
    float: left;
    display: block;
    margin:5px 0px 0 3px;
  }
  #recruitlist #selectionindexlist .cell i.custom{
    width: 30px;
    height: 30px;
    background-position: center center;
    background-repeat: no-repeat;
    float: center;
    display: block;
    margin:0 auto;
  }
  #recruitlist .el-step__title.is-finish {
    color: #409EFF;
  }
  #recruitlist .el-step__title {
    font-size:12px;
    line-height: 22px;
    padding-left: 8px;
  }
  #recruitlist .el-steps{
    margin:10px 0;
  }
  .el-tooltip__popper.is-dark{
    font-size:var(--size18) !important;
  }
  .el-tabs--card>.el-tabs__header {
    border-bottom: 1px solid #9CA3B3;
  }
  .el-tabs--card>.el-tabs__header .el-tabs__nav {
    border: 1px solid #9CA3B3;
    border-bottom: 0;
  }
  .el-tabs--card>.el-tabs__header .el-tabs__item {
    border-left: 1px solid #9CA3B3;
    /* background-color: #F0F0F0; */
  }
  .el-tabs__item.is-active {
    color: #0000FF !important;
    font-weight: bolder !important;
    /* background-color: #fff !important; */
    /* border-bottom: 2px solid #9CA3B3 !important; */
  }
</style>
<style scoped>
.mytable{
  border-top: 1px solid #74a6cc;
  border-left: 1px solid #74a6cc;
  margin:0 10px 10px;
  width: calc(100% - 20px);
}
.mytable th{
  padding: 10px;
  border-right: 1px solid #74a6cc;
  border-bottom: 1px solid #74a6cc;
  vertical-align: top;
  line-height: 150%;
  background: #FAFAFA;
  width:250px;
  text-align:right;
}
.mytable td{
  padding: 10px;
  border-right: 1px solid #74a6cc;
  border-bottom: 1px solid #74a6cc;
  vertical-align: top;
  line-height: 150%;
}
  .el-tooltip__popper{
    font-size:var(--size) !important;
  }
  h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  .wboxs{
    padding:5px 15px;
    width: calc(100% - 30px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
  .wboxd{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
